
-- --------------------------------------------------
-- Entity Designer DDL Script for SQL Server 2005, 2008, and Azure
-- --------------------------------------------------
-- Date Created: 07/18/2011 16:07:53
-- Generated from EDMX file: C:\VSProjects\VS2010\DevForce\CoProject\CoProjectDF\CoProjectModel.Desktop\CoProjectModel.edmx
-- --------------------------------------------------

SET QUOTED_IDENTIFIER OFF;
GO
USE [CoProjectDF];
GO
IF SCHEMA_ID(N'dbo') IS NULL EXECUTE(N'CREATE SCHEMA [dbo]');
GO

-- --------------------------------------------------
-- Dropping existing FOREIGN KEY constraints
-- --------------------------------------------------

IF OBJECT_ID(N'[dbo].[FK_Categories_Projects]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[Categories] DROP CONSTRAINT [FK_Categories_Projects];
GO
IF OBJECT_ID(N'[dbo].[FK_Messages_Categories]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[Messages] DROP CONSTRAINT [FK_Messages_Categories];
GO
IF OBJECT_ID(N'[dbo].[FK_MessageComments_Messages]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[MessageComments] DROP CONSTRAINT [FK_MessageComments_Messages];
GO
IF OBJECT_ID(N'[dbo].[FK_Messages_Milestones]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[Messages] DROP CONSTRAINT [FK_Messages_Milestones];
GO
IF OBJECT_ID(N'[dbo].[FK_Messages_Projects]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[Messages] DROP CONSTRAINT [FK_Messages_Projects];
GO
IF OBJECT_ID(N'[dbo].[FK_Milestones_Projects]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[Milestones] DROP CONSTRAINT [FK_Milestones_Projects];
GO
IF OBJECT_ID(N'[dbo].[FK_ToDoLists_Projects]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[ToDoLists] DROP CONSTRAINT [FK_ToDoLists_Projects];
GO
IF OBJECT_ID(N'[dbo].[FK_ToDoItems_ToDoLists]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[ToDoItems] DROP CONSTRAINT [FK_ToDoItems_ToDoLists];
GO
IF OBJECT_ID(N'[dbo].[FK_MessageComments_Users]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[MessageComments] DROP CONSTRAINT [FK_MessageComments_Users];
GO
IF OBJECT_ID(N'[dbo].[FK_Messages_Users]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[Messages] DROP CONSTRAINT [FK_Messages_Users];
GO
IF OBJECT_ID(N'[dbo].[FK_Milestones_Users]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[Milestones] DROP CONSTRAINT [FK_Milestones_Users];
GO
IF OBJECT_ID(N'[dbo].[FK_ToDoItems_Users]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[ToDoItems] DROP CONSTRAINT [FK_ToDoItems_Users];
GO

-- --------------------------------------------------
-- Dropping existing tables
-- --------------------------------------------------

IF OBJECT_ID(N'[dbo].[Categories]', 'U') IS NOT NULL
    DROP TABLE [dbo].[Categories];
GO
IF OBJECT_ID(N'[dbo].[MessageComments]', 'U') IS NOT NULL
    DROP TABLE [dbo].[MessageComments];
GO
IF OBJECT_ID(N'[dbo].[Messages]', 'U') IS NOT NULL
    DROP TABLE [dbo].[Messages];
GO
IF OBJECT_ID(N'[dbo].[Milestones]', 'U') IS NOT NULL
    DROP TABLE [dbo].[Milestones];
GO
IF OBJECT_ID(N'[dbo].[Projects]', 'U') IS NOT NULL
    DROP TABLE [dbo].[Projects];
GO
IF OBJECT_ID(N'[dbo].[ToDoItems]', 'U') IS NOT NULL
    DROP TABLE [dbo].[ToDoItems];
GO
IF OBJECT_ID(N'[dbo].[ToDoLists]', 'U') IS NOT NULL
    DROP TABLE [dbo].[ToDoLists];
GO
IF OBJECT_ID(N'[dbo].[Users]', 'U') IS NOT NULL
    DROP TABLE [dbo].[Users];
GO

-- --------------------------------------------------
-- Creating all tables
-- --------------------------------------------------

-- Creating table 'Categories'
CREATE TABLE [dbo].[Categories] (
    [CategoryID] int IDENTITY(1,1) NOT NULL,
    [ProjectID] int  NOT NULL,
    [Name] nvarchar(200)  NOT NULL,
    [CreatedDate] datetime  NOT NULL,
    [UpdatedDate] datetime  NULL
);
GO

-- Creating table 'MessageComments'
CREATE TABLE [dbo].[MessageComments] (
    [MessageCommentID] int IDENTITY(1,1) NOT NULL,
    [MessageID] int  NOT NULL,
    [UserID] int  NOT NULL,
    [Comment] nvarchar(max)  NOT NULL,
    [CreatedDate] datetime  NOT NULL,
    [UpdatedDate] datetime  NULL
);
GO

-- Creating table 'Messages'
CREATE TABLE [dbo].[Messages] (
    [MessageID] int IDENTITY(1,1) NOT NULL,
    [ProjectID] int  NOT NULL,
    [UserID] int  NOT NULL,
    [CategoryID] int  NULL,
    [MilestoneID] int  NULL,
    [Title] nvarchar(200)  NOT NULL,
    [IsPrivate] bit  NOT NULL,
    [CreatedDate] datetime  NOT NULL,
    [UpdatedDate] datetime  NULL
);
GO

-- Creating table 'Milestones'
CREATE TABLE [dbo].[Milestones] (
    [MilestoneID] int IDENTITY(1,1) NOT NULL,
    [ProjectID] int  NOT NULL,
    [UserID] int  NULL,
    [Title] nvarchar(200)  NOT NULL,
    [DueDate] datetime  NULL,
    [IsCompleted] bit  NOT NULL,
    [CreatedDate] datetime  NOT NULL,
    [UpdatedDate] datetime  NULL
);
GO

-- Creating table 'Projects'
CREATE TABLE [dbo].[Projects] (
    [ProjectID] int IDENTITY(1,1) NOT NULL,
    [Name] nvarchar(50)  NOT NULL,
    [Announcement] nvarchar(max)  NULL,
    [CreatedDate] datetime  NOT NULL,
    [UpdatedDate] datetime  NULL
);
GO

-- Creating table 'ToDoItems'
CREATE TABLE [dbo].[ToDoItems] (
    [ToDoItemID] int IDENTITY(1,1) NOT NULL,
    [ToDoListID] int  NOT NULL,
    [UserID] int  NULL,
    [Content] nvarchar(200)  NOT NULL,
    [DueDate] datetime  NULL,
    [CreatedDate] datetime  NOT NULL,
    [UpdatedDate] datetime  NULL
);
GO

-- Creating table 'ToDoLists'
CREATE TABLE [dbo].[ToDoLists] (
    [ToDoListID] int IDENTITY(1,1) NOT NULL,
    [ProjectID] int  NULL,
    [Name] nvarchar(200)  NOT NULL,
    [Description] nvarchar(max)  NOT NULL,
    [CreatedDate] datetime  NOT NULL,
    [UpdatedDate] datetime  NULL
);
GO

-- Creating table 'Users'
CREATE TABLE [dbo].[Users] (
    [UserID] int IDENTITY(1,1) NOT NULL,
    [UserName] nvarchar(max)  NOT NULL,
    [Password] nvarchar(max)  NOT NULL,
    [FriendlyName] nvarchar(max)  NOT NULL,
    [Email] nvarchar(max)  NOT NULL,
    [Question] nvarchar(max)  NOT NULL,
    [Answer] nvarchar(max)  NOT NULL,
    [CreatedDate] datetime  NOT NULL,
    [UpdatedDate] datetime  NULL
);
GO

-- --------------------------------------------------
-- Creating all PRIMARY KEY constraints
-- --------------------------------------------------

-- Creating primary key on [CategoryID] in table 'Categories'
ALTER TABLE [dbo].[Categories]
ADD CONSTRAINT [PK_Categories]
    PRIMARY KEY CLUSTERED ([CategoryID] ASC);
GO

-- Creating primary key on [MessageCommentID] in table 'MessageComments'
ALTER TABLE [dbo].[MessageComments]
ADD CONSTRAINT [PK_MessageComments]
    PRIMARY KEY CLUSTERED ([MessageCommentID] ASC);
GO

-- Creating primary key on [MessageID] in table 'Messages'
ALTER TABLE [dbo].[Messages]
ADD CONSTRAINT [PK_Messages]
    PRIMARY KEY CLUSTERED ([MessageID] ASC);
GO

-- Creating primary key on [MilestoneID] in table 'Milestones'
ALTER TABLE [dbo].[Milestones]
ADD CONSTRAINT [PK_Milestones]
    PRIMARY KEY CLUSTERED ([MilestoneID] ASC);
GO

-- Creating primary key on [ProjectID] in table 'Projects'
ALTER TABLE [dbo].[Projects]
ADD CONSTRAINT [PK_Projects]
    PRIMARY KEY CLUSTERED ([ProjectID] ASC);
GO

-- Creating primary key on [ToDoItemID] in table 'ToDoItems'
ALTER TABLE [dbo].[ToDoItems]
ADD CONSTRAINT [PK_ToDoItems]
    PRIMARY KEY CLUSTERED ([ToDoItemID] ASC);
GO

-- Creating primary key on [ToDoListID] in table 'ToDoLists'
ALTER TABLE [dbo].[ToDoLists]
ADD CONSTRAINT [PK_ToDoLists]
    PRIMARY KEY CLUSTERED ([ToDoListID] ASC);
GO

-- Creating primary key on [UserID] in table 'Users'
ALTER TABLE [dbo].[Users]
ADD CONSTRAINT [PK_Users]
    PRIMARY KEY CLUSTERED ([UserID] ASC);
GO

-- --------------------------------------------------
-- Creating all FOREIGN KEY constraints
-- --------------------------------------------------

-- Creating foreign key on [ProjectID] in table 'Categories'
ALTER TABLE [dbo].[Categories]
ADD CONSTRAINT [FK_Categories_Projects]
    FOREIGN KEY ([ProjectID])
    REFERENCES [dbo].[Projects]
        ([ProjectID])
    ON DELETE CASCADE ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_Categories_Projects'
CREATE INDEX [IX_FK_Categories_Projects]
ON [dbo].[Categories]
    ([ProjectID]);
GO

-- Creating foreign key on [CategoryID] in table 'Messages'
ALTER TABLE [dbo].[Messages]
ADD CONSTRAINT [FK_Messages_Categories]
    FOREIGN KEY ([CategoryID])
    REFERENCES [dbo].[Categories]
        ([CategoryID])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_Messages_Categories'
CREATE INDEX [IX_FK_Messages_Categories]
ON [dbo].[Messages]
    ([CategoryID]);
GO

-- Creating foreign key on [MessageID] in table 'MessageComments'
ALTER TABLE [dbo].[MessageComments]
ADD CONSTRAINT [FK_MessageComments_Messages]
    FOREIGN KEY ([MessageID])
    REFERENCES [dbo].[Messages]
        ([MessageID])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_MessageComments_Messages'
CREATE INDEX [IX_FK_MessageComments_Messages]
ON [dbo].[MessageComments]
    ([MessageID]);
GO

-- Creating foreign key on [MilestoneID] in table 'Messages'
ALTER TABLE [dbo].[Messages]
ADD CONSTRAINT [FK_Messages_Milestones]
    FOREIGN KEY ([MilestoneID])
    REFERENCES [dbo].[Milestones]
        ([MilestoneID])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_Messages_Milestones'
CREATE INDEX [IX_FK_Messages_Milestones]
ON [dbo].[Messages]
    ([MilestoneID]);
GO

-- Creating foreign key on [ProjectID] in table 'Messages'
ALTER TABLE [dbo].[Messages]
ADD CONSTRAINT [FK_Messages_Projects]
    FOREIGN KEY ([ProjectID])
    REFERENCES [dbo].[Projects]
        ([ProjectID])
    ON DELETE CASCADE ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_Messages_Projects'
CREATE INDEX [IX_FK_Messages_Projects]
ON [dbo].[Messages]
    ([ProjectID]);
GO

-- Creating foreign key on [ProjectID] in table 'Milestones'
ALTER TABLE [dbo].[Milestones]
ADD CONSTRAINT [FK_Milestones_Projects]
    FOREIGN KEY ([ProjectID])
    REFERENCES [dbo].[Projects]
        ([ProjectID])
    ON DELETE CASCADE ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_Milestones_Projects'
CREATE INDEX [IX_FK_Milestones_Projects]
ON [dbo].[Milestones]
    ([ProjectID]);
GO

-- Creating foreign key on [ProjectID] in table 'ToDoLists'
ALTER TABLE [dbo].[ToDoLists]
ADD CONSTRAINT [FK_ToDoLists_Projects]
    FOREIGN KEY ([ProjectID])
    REFERENCES [dbo].[Projects]
        ([ProjectID])
    ON DELETE CASCADE ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_ToDoLists_Projects'
CREATE INDEX [IX_FK_ToDoLists_Projects]
ON [dbo].[ToDoLists]
    ([ProjectID]);
GO

-- Creating foreign key on [ToDoListID] in table 'ToDoItems'
ALTER TABLE [dbo].[ToDoItems]
ADD CONSTRAINT [FK_ToDoItems_ToDoLists]
    FOREIGN KEY ([ToDoListID])
    REFERENCES [dbo].[ToDoLists]
        ([ToDoListID])
    ON DELETE CASCADE ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_ToDoItems_ToDoLists'
CREATE INDEX [IX_FK_ToDoItems_ToDoLists]
ON [dbo].[ToDoItems]
    ([ToDoListID]);
GO

-- Creating foreign key on [UserID] in table 'MessageComments'
ALTER TABLE [dbo].[MessageComments]
ADD CONSTRAINT [FK_MessageComments_Users]
    FOREIGN KEY ([UserID])
    REFERENCES [dbo].[Users]
        ([UserID])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_MessageComments_Users'
CREATE INDEX [IX_FK_MessageComments_Users]
ON [dbo].[MessageComments]
    ([UserID]);
GO

-- Creating foreign key on [UserID] in table 'Messages'
ALTER TABLE [dbo].[Messages]
ADD CONSTRAINT [FK_Messages_Users]
    FOREIGN KEY ([UserID])
    REFERENCES [dbo].[Users]
        ([UserID])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_Messages_Users'
CREATE INDEX [IX_FK_Messages_Users]
ON [dbo].[Messages]
    ([UserID]);
GO

-- Creating foreign key on [UserID] in table 'Milestones'
ALTER TABLE [dbo].[Milestones]
ADD CONSTRAINT [FK_Milestones_Users]
    FOREIGN KEY ([UserID])
    REFERENCES [dbo].[Users]
        ([UserID])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_Milestones_Users'
CREATE INDEX [IX_FK_Milestones_Users]
ON [dbo].[Milestones]
    ([UserID]);
GO

-- Creating foreign key on [UserID] in table 'ToDoItems'
ALTER TABLE [dbo].[ToDoItems]
ADD CONSTRAINT [FK_ToDoItems_Users]
    FOREIGN KEY ([UserID])
    REFERENCES [dbo].[Users]
        ([UserID])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_ToDoItems_Users'
CREATE INDEX [IX_FK_ToDoItems_Users]
ON [dbo].[ToDoItems]
    ([UserID]);
GO

-- --------------------------------------------------
-- Script has ended
-- --------------------------------------------------